package com.lpf.方法引用与构造器引用;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import org.junit.Test;

import com.lpf.common.Employee;

@SuppressWarnings("ALL")
public class MethodRef {

	/**
	 * 对象的引用 :: 实例方法名
	 */
	// 示例一
	@Test
	public void test1() {
		Employee emp = new Employee(101, "张三", 18, 9999.99);

		Supplier<String> sup = () -> emp.getName();
		System.out.println(sup.get());

		System.out.println("----------------------------------");

		Supplier<String> sup2 = emp::getName;
		System.out.println(sup2.get());
	}

	// 示例二
	@Test
	public void test2() {
		PrintStream ps = System.out;
		Consumer<String> con = (str) -> ps.println(str);
		con.accept("Hello World！");

		System.out.println("--------------------------------");

		Consumer<String> con2 = ps::println;
		con2.accept("Hello Java8！");

		Consumer<String> con3 = System.out::println;
		con3.accept("Hello World！");
	}

	// ===============================================================================

	/**
	 * 类名 :: 静态方法名
	 */
	// 示例一
	@Test
	public void test3() {
		Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
		System.out.println(com.compare(1, 2));

		System.out.println("-------------------------------------");

		Comparator<Integer> com2 = Integer::compare;
		System.out.println(com2.compare(1, 2));
	}

	// 示例二
	@Test
	public void test4() {
		BiFunction<Double, Double, Double> fun = (x, y) -> Math.max(x, y);
		System.out.println(fun.apply(1.5, 22.2));

		System.out.println("--------------------------------------------------");

		BiFunction<Double, Double, Double> fun2 = Math::max;
		System.out.println(fun2.apply(1.2, 1.5));
	}

	// ===============================================================================

	/**
	 * 类名 :: 实例方法名
	 */
	@Test
	public void test5() {
		BiPredicate<String, String> bp = (x, y) -> x.equals(y);
		System.out.println(bp.test("abcde", "abcde"));

		System.out.println("-----------------------------------------");

		BiPredicate<String, String> bp2 = String::equals;
		System.out.println(bp2.test("abc", "abc"));

		System.out.println("-----------------------------------------");

		Function<Employee, String> fun = (e) -> e.show();
		System.out.println(fun.apply(new Employee()));

		System.out.println("-----------------------------------------");

		Function<Employee, String> fun2 = Employee::show;
		System.out.println(fun2.apply(new Employee()));

	}

}
